﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using CodeGenerator.Common;
using CodeGenerator.Core;

namespace CodeGenerator.UI
{
    public partial class FrmCustomedTemplate : BaseUI.BaseForm
    {
        public FrmCustomedTemplate()
        {
            InitializeComponent();
        }

        #region Method
        /// <summary>
        /// 生成文件树
        /// </summary>
        /// <param name="filePath">路径</param>
        /// <param name="node">节点</param>
        private void GetFileInfo(string filePath, TreeNode node)
        {
            DirectoryInfo directoryInfo = new DirectoryInfo(filePath);
            foreach (DirectoryInfo childDirectoryInfo in directoryInfo.GetDirectories())
            {
                TreeNode directoryNode = new TreeNode(childDirectoryInfo.Name);

                TemplateFiles entity = new TemplateFiles();
                entity.FileName = childDirectoryInfo.Name;
                entity.FullName = childDirectoryInfo.FullName;
                entity.Extension = "";

                directoryNode.ImageIndex = 0;
                directoryNode.SelectedImageIndex = 0;
                directoryNode.Text = childDirectoryInfo.Name;
                directoryNode.Tag = entity;

                GetFileInfo(filePath + "\\" + childDirectoryInfo.Name, directoryNode);
                node.Nodes.Add(directoryNode);
            }
            foreach (FileInfo fileInfo in directoryInfo.GetFiles("*.*"))
            {
                TreeNode leafNode = new TreeNode();

                TemplateFiles entity = new TemplateFiles();
                entity.FileName = fileInfo.Name;
                entity.FullName = fileInfo.FullName;
                entity.Extension = fileInfo.Extension;

                leafNode.Text = leafNode.Name;
                leafNode.Tag = entity;

                if (fileInfo.Extension.TrimStart('.') == CodeGenerator.Core.FileType.cs.ToString())
                {
                    leafNode.ImageIndex = 1;
                    leafNode.SelectedImageIndex = 1;
                }
                else if (fileInfo.Extension.TrimStart('.') == CodeGenerator.Core.FileType.sql.ToString())
                {
                    leafNode.ImageIndex = 2;
                    leafNode.SelectedImageIndex = 2;
                }
                else
                {
                    leafNode.ImageIndex = 3;
                    leafNode.SelectedImageIndex = 3;
                }

                leafNode.Text = fileInfo.Name;
                leafNode.Tag = entity;
                node.Nodes.Add(leafNode);
            }
        }

        /// <summary>
        /// 绑定文件树
        /// </summary>
        private void BindFileTree()
        {
            treeView1.Nodes.Clear();
            string path = Utils.GetAppPath() + "\\CustomedTemplate";
            DirectoryUtil.CreateDir(path);

            TemplateFiles entity = new TemplateFiles();
            entity.FileName = "CustomedTemplate";
            entity.FullName = path;
            entity.Extension = "";

            TreeNode treenode = new TreeNode();
            treenode.Text = path;
            treenode.Tag = entity;
            GetFileInfo(path, treenode);
            this.treeView1.Nodes.Add(treenode);

            treeView1.ExpandAll();
        }


        private bool FileOperate(Core.FileOperateType operateType)
        {
            FrmFileOperation form = null;
            string path = "";

            TreeNode tn = treeView1.SelectedNode;
            if (tn == null) return false;

            TemplateFiles entity = (TemplateFiles)tn.Tag;


            form = new FrmFileOperation(entity, operateType);
            form.ShowDialog();
            return form.flag;
        }
        #endregion

        private void CustomedTemplate_Load(object sender, EventArgs e)
        {
            BindFileTree();
        }

        private void tsmi_Refresh_Click(object sender, EventArgs e)
        {
            BindFileTree();
        }

        private void tsmi_AddFile_Click(object sender, EventArgs e)
        {
            if (FileOperate(Core.FileOperateType.AddFile))
                BindFileTree();
        }

        private void tsmi_Rename_Click(object sender, EventArgs e)
        {
            if (FileOperate(Core.FileOperateType.Rename))
                BindFileTree();
        }

        private void tsmi_AddDirection_Click(object sender, EventArgs e)
        {
            if (FileOperate(Core.FileOperateType.AddDirection))
                BindFileTree();
        }

        private void tsmi_DelFile_Click(object sender, EventArgs e)
        {
            TreeNode tn = treeView1.SelectedNode;
            if (tn != null)
            {
                if(MessageUtil.ShowYesNoAndTips("删除不可逆,确定要删除?")== System.Windows.Forms.DialogResult.Yes)
                {
                    TemplateFiles entity = (TemplateFiles)tn.Tag;

                    if (entity.FileType == FileType.empty)
                    {
                        DirectoryUtil.DeleteDirectory(entity.FullName);
                        BindFileTree();
                    }
                    else
                    {
                        DirectoryUtil.DeleteFile(entity.FullName);
                        BindFileTree();
                    }
                }
               
            }
            else
            {
                MessageUtil.ShowTips("请先选择要删除的文件");
            }
        }

        private void treeView1_DoubleClick(object sender, EventArgs e)
        {
            TreeNode tn = treeView1.SelectedNode;
            if (tn == null || tn.GetNodeCount(false)>0) return;

            TemplateFiles entity = (TemplateFiles)tn.Tag;
            if (entity.FileType == Core.FileType.cs)
            {
                
            }

            FrmTempleteCode form = new FrmTempleteCode(entity);
            form.Text = entity.FullName;
            ((FrmMain)(this.ParentForm)).ShowContent(form.Text, form, true);
        }

        private void treeView1_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                //确定右键的位置  
                Point clickPoint = new Point(e.X, e.Y);
                //在确定后的位置上面定义一个节点  
                TreeNode selNode = treeView1.GetNodeAt(clickPoint);
                if (selNode != null)
                {
                    treeView1.SelectedNode = selNode;
                 
                }
            }
        }   
    }
}
